package com.itheima.dbsharding.month.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator.
 */
@Mapper
@Component
public interface OrderDao {

    /**
     * 插入订单
     *
     * @param price
     * @param userId
     * @param status
     * @return
     */
    @Insert("insert into t_order(price,user_id,status,crt_time)values(#{price},#{userId},#{status},#{crtTime})")
    int insertOrder(@Param("price") BigDecimal price, @Param("userId") Long userId,
                    @Param("status") String status, @Param("crtTime") Date crtTime);

    /**
     * 根据id列表查询订单
     *
     * @return
     */
    @Update("<script>" +
            " update t_order t " +
            " set price = #{price}" +
            " where " +
            " crt_time between #{startCrtTime}" +
            " and #{endCrtTime}" +
            "</script>")
    void updateOrder(@Param("price") BigDecimal price, @Param("startCrtTime")Date startCrtTime,@Param("endCrtTime")Date endCrtTime);


    /**
     * 根据id列表查询订单
     *
     * @param orderIds
     * @return
     */
    @Select("<script>" +
            "select" +
            " * " +
            " from t_order t " +
            " where t.order_id in " +
            " <foreach collection='orderIds' open='(' separator=',' close=')' item='id'>" +
            " #{id} " +
            " </foreach>" +
            " and crt_time <![CDATA[ >= ]]> #{startCrtTime}" +
            " and crt_time <![CDATA[ <= ]]> #{endCrtTime}" +
            "</script>")
    List<Map> selectOrderbyIds(@Param("orderIds") List<Long> orderIds, @Param("startCrtTime")Date startCrtTime,@Param("endCrtTime")Date endCrtTime);

    /**
     * 根据id列表查询订单
     *
     * @param orderIds
     * @return
     */
    @Select("<script>" +
            "select" +
            " * " +
            " from t_order t " +
            " where " +
            "  crt_time <![CDATA[ >= ]]> #{startCrtTime}" +
            " and crt_time <![CDATA[ <= ]]> #{endCrtTime}" +
            "</script>")
    List<Map> selectOrderbyTime(@Param("startCrtTime")Date startCrtTime,@Param("endCrtTime")Date endCrtTime);

    /**
     * 根据id列表查询订单
     *
     * @param orderIds
     * @return
     */
    @Select("<script>" +
            "select" +
            " * " +
            " from t_order t " +
            " where " +
            "  crt_time between #{startCrtTime}" +
            " and #{endCrtTime}" +
            "</script>")
    List<Map> selectbyBetweenTime(@Param("startCrtTime")Date startCrtTime,@Param("endCrtTime")Date endCrtTime);

    /**
     * 根据id列表查询订单
     *
     * @param orderIds
     * @return
     */
    @Select("<script>" +
            "select" +
            " * " +
            " from t_order t " +
            " where " +
            "  crt_time <![CDATA[ = ]]> #{crtTime}" +
            "</script>")
    List<Map> selectByTime(@Param("crtTime")Date crtTime);

    /**
     * 根据id列表和用户id查询订单
     *
     * @param orderIds
     * @return
     */
    @Select("<script>" +
            "select" +
            " * " +
            " from t_order t " +
            " where t.order_id in " +
            " <foreach collection='orderIds' open='(' separator=',' close=')' item='id'>" +
            " #{id} " +
            " </foreach>" +
            " and user_id = #{userId} " +
            "</script>")
    List<Map> selectOrderbyUserAndIds(@Param("userId") Long userId, @Param("orderIds") List<Long> orderIds);
}
